package com.colorful.insuranceproject.claims;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.colorful.insuranceproject.R;
import com.colorful.insuranceproject.commonActivity.WebViewActivity;
import com.colorful.insuranceproject.consts.URLConst;
import com.colorful.insuranceproject.dialog.ChoiceDateDialog;
import com.lovely3x.common.fragments.BaseCommonFragment;
import com.lovely3x.common.managements.user.UserManager;
import com.lovely3x.loginandresgiter.bean.SimpleUser;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by handsome-Bob on 2017/11/7.
 * <p>
 * 理赔设置的Fragment
 */
public class StepOneFragment extends BaseCommonFragment {

    private static final int REQUEST_CODE = 10000;

    /**
     * 日期
     */
    @Bind(R.id.tv_date)
    TextView tvDate;

    /**
     * 理赔人员
     */
    @Bind(R.id.tv_person)
    TextView tvPerson;

    public static StepOneFragment newInstance() {
        Bundle bundle = new Bundle();
        StepOneFragment fragment = new StepOneFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getContentView() {
        return R.layout.activity_claims_step_one;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this, mRootView);
    }

    @Override
    protected void onViewInitialized() {
        SimpleUser users = (SimpleUser) UserManager.getInstance().getCurrentUser();
        tvPerson.setText(users.getUserName());
    }

    @Override
    protected void restoreInstanceOnCreateBefore(Bundle savedInstanceState) {

    }

    @Override
    protected void restoreInstanceOnCreateAfter(Bundle savedInstanceState) {

    }

    @OnClick({R.id.tv_claims_encounter_question, R.id.tv_set, R.id.rl_date})
    void OnClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_claims_encounter_question:
                String url = URLConst.CLAIM_SETTLEMENT_AGREEMENT;
                mActivity.launchActivity(WebViewActivity.class, WebViewActivity.EXTRA_LOAD_URL, url);
                break;
            case R.id.tv_set:
                if (tvDate.getText().equals(getString(R.string.please_select))) {
                    mActivity.showToast(getString(R.string.please_choose_date));
                } else {
                    mActivity.launchActivityForResult(ClaimsStepTwoActivity.class, REQUEST_CODE, ClaimsStepTwoActivity.EXTRA_DATE,
                            tvDate.getText().toString(), ClaimsStepTwoActivity.EXTRA_NAME, tvPerson.getText().toString());
                }
                break;
            case R.id.rl_date:
                ChoiceDateDialog cdd = new ChoiceDateDialog();
                Bundle bundle = new Bundle();
                Calendar calendar = Calendar.getInstance();
                bundle.putInt(ChoiceDateDialog.EXTRA_MAX_YEAR, calendar.get(Calendar.YEAR));
                bundle.putInt(ChoiceDateDialog.EXTRA_MAX_MONTH, calendar.get(Calendar.MONTH));
                bundle.putInt(ChoiceDateDialog.EXTRA_MAX_DAY, calendar.get(Calendar.DAY_OF_MONTH));
                cdd.setArguments(bundle);
                cdd.setOnChoiceChangedListener(new ChoiceDateDialog.OnChoiceStateChangedListener() {
                    @Override
                    public void onChoiceCancel() {

                    }

                    @Override
                    public void onDateChoiced(int year, int month, int day, long millisecond) {
                        try {
                            // Ignore
                        } catch (Exception e) {
                            e.printStackTrace();
                            mActivity.showToast(R.string.you_inputed_date_is_invalidate);
                        }
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
                        tvDate.setText(simpleDateFormat.format(millisecond));
                    }
                });
                cdd.show(mActivity.getSupportFragmentManager(), cdd.getClass().getSimpleName());
                break;
        }
    }
}
